<?php

	class Department extends Department_model {

		function __construct() {
			parent::__construct();
		}
                
                public static function getList() {
                    
                    $department = new Department();
                    $department->addWhere('department.disabled = '.IS_NOT_DISABLED);
                    $department->orderBy(getI18nRealStringSql('department.name'));
                    $department->find();
                    return $department;
                    
                }
                function getPagination(&$filter = array()){
                     $department = new Department();
            $filter[PAGINATION_QUERY_STRING_SEGMENT] = isset($filter[PAGINATION_QUERY_STRING_SEGMENT]) && $filter[PAGINATION_QUERY_STRING_SEGMENT] ? $filter[PAGINATION_QUERY_STRING_SEGMENT] : 1;
            $department->addSelect();
            $department->addSelect('COUNT(*) count');
            $department->addwhere('disabled = 0');
            $department->find();
            $department->fetchNext();
            
            //install paginantion
            
            $this->load->library('pagination');
            $this->pagination->total_rows = $department->count;
            $this->pagination->url = current_url();
            $this->pagination->cur_page = $filter[PAGINATION_QUERY_STRING_SEGMENT];
            $department->addSelect();
            $department->addSelect('department.*');
            $department->limit($this->pagination->first_row .", ".$this->pagination->rows_per_page);
            $department->find();
        
            return $department;
            
                }
                function hasUser($id){
                    
                    $user_department = new UserDepartment();
                    $user_department->addSelect();
                    $user_department->addSelect('COUNT(*) count');
                    $user_department->addWhere('user_department.id_department = '.$id);
                    $user_department->find();
                    $user_department->fetchNext();
                    
                    return $user_department->count > 0 ? true : false;
                    
                }
            function delete() {
            
            if ($this->id == 1)
                return false;
            
            $department = new Department();
            $department->get($this->id);
            $department->disabled = IS_DISABLED;
            $department->update();
            
            }
	}
